package zip;

import org.apache.commons.net.ftp.FTPClient;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ye on 2016/3/16.
 */
public class Main {

    public static void main(String[] args) throws Exception {
//        HdfsReader hdfsReader = new HdfsReader("hdfs://hadoop01:9000/user/hadoop/test1/input/in");
//        hdfsReader.run("D:/zhangye/");
        FtpUtil2.connectServer("192.168.233.139", 21, "hadoop", "hadoop123", "/home/hadoop/zhangye");

        String path1 = "D://test1.txt";
        String path2 = "D://test2.txt";
        List<String> list = new ArrayList<String>();
        list.add(path1);
//        String path = path1;
        list.add(path2);
        for (String path : list) {


            FileInputStream in = new FileInputStream(path);
            BufferedReader lineRead = new BufferedReader(new InputStreamReader(in));

            //���gz�ļ���
            String fileNamePre = path.substring(4, path.lastIndexOf(".txt"));

        System.out.println(fileNamePre);
            int gzCount = 0;
            int lineLimit = 1000;
            String line = "";

            String gzFileName = "";
            int temp = 0;
            OutputStream out = null;
            byte[] gzBuf;
            while ((line = lineRead.readLine()) != null) {
                gzFileName = fileNamePre + "_" + gzCount + ".gz";
                temp++;

                if (temp == 1) {
                    out = FtpUtil2.uploadGzFileStream2(gzFileName, line.getBytes());
                } else if (temp == lineLimit) {
                    FtpUtil2.uploadGzFileStream3(out, gzFileName, line.getBytes());

                    temp = 0;
                    gzCount++;
                    out = null;
                } else {
//                gzBuf=GzUtil.gZip(line.getBytes(),line.getBytes().length);
//                out.write(gzBuf, 0, gzBuf.length);
                    FtpUtil2.appendFileStream2(out, gzFileName, line.getBytes());
                }
            }
            lineRead.close();
            in.close();
            in = null;
            out.close();
            // Must call completePendingCommand() to finish command.
            if(!FtpUtil2.ftpClient.completePendingCommand()) {
                FtpUtil2.ftpClient.logout();
                FtpUtil2.ftpClient.disconnect();
                throw new Exception("�ϴ��ļ�["+gzFileName+"]�е��ļ�����!");
            }

//        FtpUtil2 ftpUtil = new FtpUtil2();
//        ftpUtil.upload("D:/zhangye/1.txt.gz","ftp.properties");

//        HDFSReader hdfsReader = new HDFSReader(args[0]);
//        hdfsReader.run( args[1]);
        }
    }
}
